from django.shortcuts import redirect
from django.views.generic import TemplateView, View
from google.appengine.api import users
from apps.blog.models import Article


class LoginRequiredMixin(View):
    """ It's simple mixin for checking user authentication. """

    def dispatch(self, request, *args, **kwargs):
        if not users.is_current_user_admin():
            return redirect(users.create_login_url(request.get_full_path()))

        return super(LoginRequiredMixin, self).dispatch(
            request, *args, **kwargs)


class CoreIndex(TemplateView):
    template_name = 'core/index.html'

    def get_context_data(self, **kwargs):
        articles = Article.all().filter('is_published', True).order(
            '-updated_at')
        data = super(CoreIndex, self).get_context_data(**kwargs)
        data.update({
            'articles': articles
        })

        return data
